import { IUpdateInfo, ILoginParams, IUserInfoVo, IUserLogin, IWxLoginParams } from './login.typings'
import { http } from '@/utils/http'

/**
 * 登录表单
 */
export interface ILoginForm {
  phone: string
  password: string
}

/**
 * 获取用户信息
 */
export const getUserInfo = (data) => {
  console.log('查看参数', data)

  return http.post<IUserInfoVo>(
    '/user/info',
    data,
    {},
    { Authorization: 'Bearer ' + uni.getStorageSync('access_token') },
  )
}

/**
 * 修改用户信息
 */
export const updateInfo = (data: IUpdateInfo) => {
  return http.post(
    '/company/save',
    data,
    {},
    { Authorization: 'Bearer ' + uni.getStorageSync('access_token') },
  )
}

/**
 * 获取微信登录凭证
 * @returns Promise 包含微信登录凭证(code)
 */
export const getWxCode = () => {
  return new Promise<UniApp.LoginRes>((resolve, reject) => {
    uni.login({
      provider: 'weixin',
      success: (res) => resolve(res),
      fail: (err) => reject(new Error(err)),
    })
  })
}

/**
 * 微信登录
 * @param params 微信登录参数，包含code
 * @returns Promise 包含登录结果
 */
export const wxLogin = (data: IWxLoginParams) => {
  return http.post<IUserLogin>('/user/wxLogin', data)
}

/**
 * 普通登录
 * @param params
 * @returns Promise 包含登录结果
 */
export const Login = (data: ILoginParams) => {
  return http.post<IUserLogin>('/user/wxLogin', data)
}
